#include "mytextedit.h"


MyTextEdit::MyTextEdit(QWidget*& widget)
{

}

MyTextEdit::MyTextEdit()
{

}

MyTextEdit &MyTextEdit::getInstance()
{
    static MyTextEdit instance;
    return instance;
}

void MyTextEdit::insertImg(QString url)
{

        QUrl Uri (url);
        QImage image = QImageReader ( url ).read();

        QTextDocument * textDocument = document();
        textDocument->addResource( QTextDocument::ImageResource, Uri, QVariant (image));
        QTextCursor cursor = textCursor();
        QTextImageFormat imageFormat;
        imageFormat.setWidth( image.width());
        imageFormat.setHeight( image.height());
        imageFormat.setName( Uri.toString() );
        cursor.insertImage(imageFormat);
}

void MyTextEdit::getImg(QString imgname)//获取文本框中的图片
{
    QTextDocument *doc = document();
    QTextBlock block = doc->begin();
    while (block.isValid())
    {
        for (QTextBlock::iterator i = block.begin(); !i.atEnd(); ++i)
        {
             QTextCharFormat format = i.fragment().charFormat();
             bool isImage = format.isImageFormat();
             if (isImage)
             {
                 QTextImageFormat imgf = format.toImageFormat();
                 if(doc->resource(QTextDocument::ImageResource, imgf.name()).canConvert<QPixmap>())
                 {
                     QString type = doc->resource(QTextDocument::ImageResource, imgf.name()).typeName();
                     QVariant var = doc->resource(QTextDocument::ImageResource, imgf.name());
                     QString imgName = imgf.name();
                    //取出图片
                     QImage image = QImage(var.value<QImage>());
                     QPixmap map = QPixmap().fromImage(image);


                 }
             }
        }
        block = block.next();
    }
}
